package org.hb0712.sya.tools.database.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.hb0712.sya.tools.database.component.DataBaseComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class TableDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DataBaseComponent dbComponent;
    @Autowired
    private Map<String, String[]> tablenames;
    @Autowired
    private Map<String, String[]> tablenames2;
    @Autowired
    private Map<String, String[]> tablenames3;
    
    private Map<String, List<Map<String, Object>>> structuresDev;
    private Map<String, List<Map<String, Object>>> structuresProd;
    
    private Map<String, List<Map<String, Object>>> dataDev;
    private Map<String, List<Map<String, Object>>> dataProd;
    
    public List<Map<String, Object>> queryDataDev(String tablename) {
        String sql = "SELECT * FROM " + tablename;
        return dbComponent.dev.queryForList(sql);
    }
    
    public List<Map<String, Object>> queryStructureDev(String tablename) {
        String sql = "SELECT c.COLUMN_NAME, c.DATA_TYPE, c.DATA_PRECISION, c.DATA_SCALE FROM \"ALL_TAB_COLUMNS\" c WHERE c.TABLE_NAME = '"+tablename+"'";
        return dbComponent.dev.queryForList(sql);
    }
    
    public List<Map<String, Object>> getData(String tablename) {
        String sql = "SELECT c.COLUMN_NAME, c.DATA_TYPE FROM \"ALL_TAB_COLUMNS\" c WHERE c.TABLE_NAME = '"+tablename+"'";
        return jdbcTemplate.queryForList(sql);
    }
    
    public List<Map<String, Object>> queryStructureProd(String tablename) {
        String sql = "SELECT c.COLUMN_NAME, c.DATA_TYPE, c.DATA_PRECISION, c.DATA_SCALE FROM \"ALL_TAB_COLUMNS\" c WHERE c.TABLE_NAME = '"+tablename+"'";
        return dbComponent.prod.queryForList(sql);
    }
    
    public List<Map<String, Object>> queryDataProd(String tablename) {
        String sql = "SELECT * FROM " + tablename;
        return dbComponent.prod.queryForList(sql);
    }
    
    public void setStructureDev() {
        structuresDev = new HashMap<String, List<Map<String, Object>>>();
        dbComponent.dev.connect();
        for (String key:tablenames.keySet()) {
            String[] names = tablenames.get(key);
            for (String name:names) {
                var list = queryStructureDev(name);
                structuresDev.put(name, list);
            }
        }
        dbComponent.dev.close();
    }
    
    public List<Map<String, Object>> getStructureDev(String tablename) {
        if (structuresDev == null) {
            setStructureDev();
        }
        return structuresDev.get(tablename);
    }
    
    public void setStructureProd() {
        structuresProd = new HashMap<String, List<Map<String, Object>>>();
        dbComponent.prod.connect();
        for (String key:tablenames.keySet()) {
            String[] names = tablenames.get(key);
            for (String name:names) {
                var list = queryStructureProd(name);
                structuresProd.put(name, list);
            }
        }
        dbComponent.prod.close();
    }
    
    public List<Map<String, Object>> getStructureProd(String tablename) {
        if (structuresProd == null) {
            setStructureProd();
        }
        return structuresProd.get(tablename);
    }
    
    public void setDataDev() {
        dataDev = new TreeMap<String, List<Map<String, Object>>>();
        dbComponent.dev.connect();
        for (String key:tablenames2.keySet()) {
            String[] names = tablenames2.get(key);
            for (String name:names) {
                var list = queryDataDev(name);
                dataDev.put(name, list);
            }
        }
        dbComponent.dev.close();
    }
    
    public List<Map<String, Object>> getDataDev(String tablename, String TABLE_NAME) {
        if (dataDev == null) {
            setDataDev();
        }
        var list = dataDev.get(tablename);
        // 筛选后的内容
        var result = new ArrayList<Map<String, Object>>();
        var columnNames = tablenames3.get(tablename);
        for (int i=0;i<list.size();i++) {
            var item = list.get(i);
            if (TABLE_NAME.equals(item.get("TABLE_NAME"))) {
                var map = new LinkedHashMap<String, Object>();
                for (int j=0;j<columnNames.length;j++) {
                    String name = columnNames[j];
                    Object value = item.get(name);
                    map.put(name, value);
                }
                result.add(map);
            }
        }
        return result;
    }
    
    public void setDataProd() {
        dataProd = new TreeMap<String, List<Map<String, Object>>>();
        dbComponent.prod.connect();
        for (String key:tablenames2.keySet()) {
            String[] names = tablenames2.get(key);
            for (String name:names) {
                var list = queryDataProd(name);
                dataProd.put(name, list);
            }
        }
        dbComponent.prod.close();
    }
    
    public List<Map<String, Object>> getDataProd(String tablename, String TABLE_NAME) {
        if (dataProd == null) {
            setDataProd();
        }
        var list = dataProd.get(tablename);
        // 筛选后的内容
        var result = new ArrayList<Map<String, Object>>();
        var columnNames = tablenames3.get(tablename);
        for (int i=0;i<list.size();i++) {
            var item = list.get(i);
            if (TABLE_NAME.equals(item.get("TABLE_NAME"))) {
                var map = new LinkedHashMap<String, Object>();
                for (int j=0;j<columnNames.length;j++) {
                    String name = columnNames[j];
                    Object value = item.get(name);
                    map.put(name, value);
                }
                map.put("ID", item.get("ID"));
                result.add(map);
            }
        }
        return result;
    }
}
